package cn.itcast.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Map;

@Component
@ConfigurationProperties(prefix = "al")
@Data
public class AlibabaNoteUtil {

    private final static String PHONE_NUMBERS = "PhoneNumbers";
    public final static String SIGN_NAME = "SignName";
    public final static String TPL_CODE = "TemplateCode";
    private final static String TPL_PARAM = "TemplateParam";
    public final static String SIGN_NAME_VALUE = "阿里云短信测试";
    public final static String TPL_CODE_VALUE = "SMS_154950909";
    private String regionId;
    private String accessKeyId;
    private String secret;
    private String domain;
    private String version;
    private String action;


    public boolean addOrderNote(Map<String, Object> param, String phone) throws Exception {
        if (StringUtils.isEmpty(phone)) {
            return false;
        }
        //default 地域节点，默认就好  后面是 阿里云的 id和秘钥（这里记得去阿里云复制自己的id和秘钥哦）
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);
        //这里不能修改
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(domain);
        request.setVersion(version);
        request.setAction(action);

        request.putQueryParameter(PHONE_NUMBERS, phone);                    //手机号
        request.putQueryParameter(SIGN_NAME, SIGN_NAME_VALUE);    //申请阿里云 签名名称（暂时用阿里云测试的，自己还不能注册签名）
        request.putQueryParameter(TPL_CODE, TPL_CODE_VALUE); //申请阿里云 模板code（用的也是阿里云测试的）
        request.putQueryParameter(TPL_PARAM, JSONObject.toJSONString(param));

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
